<template>
  <el-dialog title="新增员工" :visible="showDialog" width="30%" @close="onClose">
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:380px" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:300px" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:220px" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:220px" placeholder="请选择">
          <!-- option组件 循环下拉数据  -->
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:380px" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:300px" placeholder="请选择部门" @focus="getDepartments" />
        <el-tree
          v-if="isShowTree"
          v-loading="loading"
          :data="deptsTree"
          :props="defaltProps"
          default-expand-all
          @node-click="onClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:220px" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="onClose">取消</el-button>
          <el-button type="primary" size="small" @click="addEmployees">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { AddEmployeesAPI } from '@/api/employees'
import { getDepartmentsAPI } from '@/api/departments'
import contantData from '@/api/constant/employees'
import { getTreeList } from '@/utils'
export default {
  props: {
    // 控制弹框显示
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  // 父组件 使用v-model 向子组件传递数据  解析成 :value  和 @input事件
  // 通过model 可以自定义父组件的 v-model 传递过来的数据名 把value改成showDialog
  // model: {
  //   prop: 'showDialog'
  // },
  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: 1,
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 10, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: ['blur', 'change'] }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间', trigger: 'blur' }]
      },
      // 聘用形式
      hireType: contantData.hireType,
      deptsTree: [],
      defaltProps: {
        label: 'name'
      },
      isShowTree: false,
      loading: false
    }
  },
  methods: {
    // 关闭对话框
    onClose() {
      this.$refs.form.resetFields()
      this.$emit('update:show-dialog', false)
      // this.$emit('input', false)
    },
    // 点击确定 添加员工
    async addEmployees() {
      try {
        await this.$refs.form.validate() // 整个表单校验
        await AddEmployeesAPI(this.formData)
        this.$message.success('添加员工成功')
        this.onClose()
        // this.$emit('refresh')
        // 当组件只有一个父组件 时 可以通过$parent调用父组件的方法
        this.$parent.getEmployeesList()
      } catch (err) {
        this.$message('表单校验不通过')
      }
    },
    // 部门输入框获取焦点
    async getDepartments() {
      if (this.deptsTree.length === 0) this.loading = true
      this.isShowTree = true
      const res = await getDepartmentsAPI()
      this.loading = false
      this.deptsTree.length === 0 ? this.deptsTree = getTreeList('', res.depts) : this.deptsTree
    },
    // 选中树形控件某一项时
    onClick(row) {
      this.isShowTree = false // 隐藏树形控件
      this.formData.departmentName = row.name
    }
  }
}
</script>

<style scoped lang="scss">
.el-tree {
  width: 395px;
  // background: rgb(156, 201, 243);
  margin-top: 10px;
  border: 1px solid #dddee1;
  border-radius: 5px;
}
</style>
